package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with LXD
 *
 * @author:Luhui
 * @qq:729913162
 * @Date:2018-07-09
 * @Time:16:05
 */
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("find/{id}")
    public User findOne(@PathVariable("id") int id){
        return userService.selectById(id);
    }

    @GetMapping("/discovery")
    public Object discoveryClient(){
        List<String> list = discoveryClient.getServices();
        System.out.println("*************"+list);
        List<ServiceInstance> instances = discoveryClient.getInstances("provider-user");
        System.out.println("===========================================");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()
                +"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }

}
